from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .serializers import AreaSerializer,AreaSubsSerializer
from .models import Area

"""
1,查询所有的省(最高行政区域)
2,查询具体的省,对应的所有的市    | 选择了对应的市,把所有市对应的县查出来
"""
class AreaViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    # serializer_class = AreaSerializer

    # queryset = Area.objects.all()
    pagination_class = None
    def get_serializer_class(self):
        if self.action == "list":
           return AreaSerializer
        else:
           return AreaSubsSerializer

    def get_queryset(self):
        if self.action == "list":
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()
